package com.hotel.BL;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.GuestType;

public class GuestTypeLogic extends AbstractBL{
	
	public GuestType GetGuestType(long id)
	{
		Key key = KeyFactory.createKey(EntityName.GuestType, id);
		try {
			Entity lang = datastore.get(key);
			GuestType c = new GuestType();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
		
	public List<HashMap<String,Object>> OnlyIdAndTitle(List<GuestType> list){
		List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
		for(int i=0;i<list.size();i++){
			HashMap<String,Object> map = new HashMap<String, Object>();
			map.put("id", list.get(i).getId());
			map.put("title", list.get(i).getTitle());
			data.add(map);
		}
		return data;
	}

	public GuestType GetGuestType(long hotelId,String title)
	{
		Query q = new Query(EntityName.GuestType);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetGuestType(datastore.prepare(q).asSingleEntity());
	}
	
	public List<GuestType> GetAllGuestType(long hotelId)
	{
		Query q = new Query(EntityName.GuestType);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("Status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("Avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public GuestType GetGuestType(Entity entity)
	{
		GuestType item = new GuestType();
		item.setEntity(entity);
		return item;
	}
	
	public List<GuestType> GetList(List<Entity> entities)
	{
		List<GuestType> list = new ArrayList<GuestType>();
		for(Entity e : entities)
		{
			GuestType a = new GuestType();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public GuestType AddGuestType(long hotelId,String title,String description) throws EntityNotFoundException{
		GuestType t = new GuestType();
		t.setHotelId(hotelId);
		t.setTitle(title);
		t.setDescription(description);
		t.setAvaliable(true);
		t.setStatus(true);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public GuestType EditGuestType(long guesttypeId,String title,String description) throws EntityNotFoundException{
		GuestType t = GetGuestType(guesttypeId);
		t.setTitle(title);
		t.setDescription(description);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public GuestType DeleteGuestType(long guesttypeId) throws EntityNotFoundException{
		GuestType t = GetGuestType(guesttypeId);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public GuestType ChangeStatus(long guesttypeId) throws EntityNotFoundException{
		
		GuestType a = GetGuestType(guesttypeId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public List<GuestType> GetAllGuestType4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.GuestType);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("Avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	
	public List<GuestType> GetAllGuestType4Admin(long hotelId)
	{
		Query q = new Query(EntityName.GuestType);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("Avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
}
